<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>定时器</title>
</head>
<body>
<h1>0</h1>
<script>
    let count = 0;
    let h = document.querySelector("h1");

    //开启定时器
    let timer1 = setInterval(f, 1000); //毫秒为单位

    function f() {
        count++;
        h.innerText = count;
        if (count == 6) {
            clearInterval(timer1); //停止定时器
        }
    }

    //匿名方法开启定时器
    let timer = setInterval(function () {
        console.log(count);
        if (count == 5) {
            clearInterval(timer); //停止定时器
        }
    }, 1000);

    //开启只执行一次的定时器
    setTimeout(function () {
        alert("时间到！")
    }, 7000);
</script>

</body>
</html>